from sqlalchemy.orm import Session
from . import models, schemas

def get_qrcode(db: Session, qrcode_id: int):
    return db.query(models.Qrcode).filter(models.Qrcode.Id == qrcode_id).first()

def get_qrcodes(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Qrcode).offset(skip).limit(limit).all()

def create_qrcode(db: Session, qrcode: schemas.QrcodeCreate):
    db_qrcode = models.Qrcode(**qrcode.dict())
    db.add(db_qrcode)
    db.commit()
    db.refresh(db_qrcode)
    return db_qrcode

def update_qrcode(db: Session, qrcode_id: int, qrcode: schemas.QrcodeCreate):
    db_qrcode = db.query(models.Qrcode).filter(models.Qrcode.Id == qrcode_id).first()
    if db_qrcode:
        for key, value in qrcode.dict().items():
            setattr(db_qrcode, key, value)
        db.commit()
        db.refresh(db_qrcode)
    return db_qrcode

def delete_qrcode(db: Session, qrcode_id: int):
    db_qrcode = db.query(models.Qrcode).filter(models.Qrcode.Id == qrcode_id).first()
    if db_qrcode:
        db.delete(db_qrcode)
        db.commit()
    return db_qrcode